# -*- coding: utf-8 -*-
__author__ = 'xuliliang'
# @Time    : 2018/7/10 下午5:09
# @Author  : 'xuliliang'
# @Email   : xuliliang@u51.com
# @File    : task.py
# @Software: PyCharm

import  time,uuid
import  saltapi
salt = saltapi.SaltApi()
from celery import Celery
app = Celery('tasks', broker='amqp://admin:adminu51@10.1.126.13/geinihua',backend='amqp://admin:adminu51@10.1.126.13/geinihua')


#####celery -A tasks worker --loglevel=info后台执行


@app.task
def update_host_info(minion):
		result = salt.get_minions_grains(minion)
		kernel = result['kernel']
		num_cpus = result['num_cpus']
		ip = result['fqdn_ip4'][0]
		mem_total = result['mem_total']
		hostname = result['host']
		info = {
			"ip": ip,
			"kernel": kernel,
			"num_cpus": num_cpus,
			"mem_total": mem_total,
			"hostname": hostname,
			"sn": sn
		}
		return  info
@app.task
def getAllHost():
	host_list = []
	host_list = salt.runner_status('status')['up']
	for host in host_list:
		result = salt.get_minions_grains(host)
		host = result['host']
		list = result['fqdn_ip4']
		host_list.append(list[0])
	return host_list

@app.task
def add(x, y):
    return x + y
if __name__ == '__main__':
	# getAllHost.delay()
	res = add.delay(8393010,23039)
	# 查看tasks运行结果
	print res.ready()
	print res.state
	#获取执行结果
	print res.get()
